﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HandyControl.Controls;
using MyHelper;
using clf.weighing.entity;
using Button = System.Windows.Controls.Button;

namespace weighing
{
    /// <summary>
    ///磅房 的交互逻辑
    /// </summary>
    public partial class ScaleHouseWindow : GlowWindow
    {
        #region variables
        public List<ScaleHouseVo>? mHouseVos;
        private bool isEdit = false;
        private bool isEditing = false;
        private ScaleHouseVo mEditHouseVo = new ScaleHouseVo(new ScaleHouse());
        #endregion


        public ScaleHouseWindow()
        {
            InitializeComponent();

            CommonFunction.ShowCustomIcon(this);
        }

        #region window event

        private void Cate_Window_Loaded(object sender, RoutedEventArgs e)
        {
        
        }
      
        private void Cate_Window_ContentRendered(object sender, EventArgs e)
        {
            LoadData();
        }
        private void Cate_Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape) this.Close();
        }
        #endregion


        private void LoadData()
        {
            this.DataLv.ItemsSource = null;
            var list = ScaleHouseDao.Instance.SelectAll();
            mHouseVos = list?.Select((i) => { return new ScaleHouseVo(i); }).ToList();
            this.DataLv.ItemsSource = mHouseVos;
        }

        private void DataLv_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddBtn_Click(object sender, RoutedEventArgs e)
        {
            if (isEditing)
            {
                return;
            }
            isEdit = false;
            this.DrawerHeaderTB.Text = "新增";
            this.AddDrawerRight.IsOpen = true;
        }

        private void EditBtn_Click(object sender, RoutedEventArgs e)
        {
            isEdit = true;
            isEditing = true;
            this.DrawerHeaderTB.Text = "修改";
            var button = sender as Button;
            var vo = button?.Tag as ScaleHouseVo;
            BindEditValue(vo?.mScaleHouse);
            mEditHouseVo = vo;
            this.AddDrawerRight.IsOpen = true;
        }

        private void DelBtn_Click(object sender, RoutedEventArgs e)
        {
            Button? button = sender as Button;
            var vo = button?.Tag as ScaleHouseVo;
            string msg = "是否要删除 ：" + vo?.Name;
            var reault = CommonFunction.Ask(msg, this);

            if (reault == MessageBoxResult.Yes)
            {
               
                var res = ScaleHouseDao.Instance.Sqlite.Delete(vo?.mScaleHouse);

                if (res == 1)
                {
                    this.mHouseVos?.Remove(vo);
                    this.DataLv.ItemsSource = null;
                    this.DataLv.ItemsSource = this.mHouseVos;
                    CommonFunction.AlertInfo("操作成功");
                }
                else if (res == -1)
                {
                    CommonFunction.AlertInfo("操作失败：该下有数据，不能删除！");
                }
                else
                {
                    CommonFunction.AlertInfo("操作失败");
                    Growl.ErrorGlobal("操作失败");
                }
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            if (isEdit)
            {
                Eidt();
            }
            else
            {
                Insert();
            }
        }

        private void Insert()
        {
            ScaleHouse house = new ScaleHouse()
            {
                name = this.nameTb.Text.Trim(),
                companyId = App.CurrentCompanyVo.Id
            };
            if (house.name.IsNullOrEmpty() || !house.name.Length.IsBetween(2, 20))
            {
                this.saveBtn.IsChecked = false;
                Growl.Error("名称的长度为2到20 个字之间");
                return;
            }
            house.nameSpell = StringHelper.GetFirstPinyin(house.name);

            if (ScaleHouseDao.Instance.IsExist(house))
            {
                this.saveBtn.IsChecked = false;
               CommonFunction.AlertInfo("已经存在！");
                return;
            }

            house.id = IdHelper.LongID;
            house.addUserId = App.CurrentUser.id;
            house.addUserName = App.CurrentUser.name;
            house.addTime = DateTime.Now;
            house.isDelete = 0;
            var res = ScaleHouseDao.Instance.Sqlite.Insert(house);

            if (res > 0)
            {
                this.saveBtn.IsChecked = false;
                Growl.SuccessGlobal("保存成功！");
                this.mHouseVos.Insert(0, new ScaleHouseVo(house));
                this.DataLv.ItemsSource = null;
                this.DataLv.ItemsSource = mHouseVos;
                ResertInput();
            }
            else
            {
                this.saveBtn.IsChecked = false;
                string msg = "保存数据失败！";
                CommonFunction.AlertInfo(msg);
            }
        }

        private void Eidt()
        {
            if (mEditHouseVo == null)
            {
                ResertInput();
                return;
            }

            String name = this.nameTb.Text.Trim();
            if (!name.Length.IsBetween(2, 10))
            {
                this.saveBtn.IsChecked = false;
                CommonFunction.AlertInfo("名称的长度为2到10 个字之间");
                return;
            }
            mEditHouseVo.Name = name;
            mEditHouseVo.NameSpell = StringHelper.GetFirstPinyin(name);
            mEditHouseVo.UpdateUserId = App.CurrentUser.id;
            mEditHouseVo.UpdateUserName = App.CurrentUser.name;
            mEditHouseVo.UpdateTime = DateTime.Now;
            var res =ScaleHouseDao.Instance.Sqlite.Update(mEditHouseVo.mScaleHouse);
            if (res > 0)
            {
                this.saveBtn.IsChecked = false;
                Growl.SuccessGlobal("保存成功！");
                LoadData();
                ResertInput();
            }
            else
            {
                this.saveBtn.IsChecked = false;
                string msg = "保存数据失败！";
                CommonFunction.AlertInfo(msg);
            }

        }
        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            isEditing = false;
            this.AddDrawerRight.IsOpen = false;
        }
        /// <summary>
        /// 修改时设置值 
        /// </summary>
        private void BindEditValue(ScaleHouse? house)
        {
            if (house != null)
            {
                this.nameTb.Text = house.name;
            }
        }

        public void ResertInput()
        {
            mEditHouseVo = null;
            this.nameTb.Text = "";
            this.AddDrawerRight.IsOpen = false;
        }

        private void RefreshBtn_Click(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void AddDrawerRight_Closed(object sender, RoutedEventArgs e)
        {
            mEditHouseVo = null;
            this.nameTb.Text = "";
            isEdit = false;
        }
    }
}
